package com.wangsd.web.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangsd.common.utils.ApplicationUtils;
import com.wangsd.web.sys.entity.SysButton;
import com.wangsd.web.sys.entity.SysMenu;
import com.wangsd.web.sys.entity.SysPermission;
import com.wangsd.web.sys.entity.SysRole;
import com.wangsd.web.sys.mapper.SysRoleMapper;
import com.wangsd.web.sys.service.SysPermissionService;
import com.wangsd.web.sys.service.SysRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author generator
 * @since 2019-09-02
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    SysRoleMapper sysRoleMapper;

    @Autowired
    SysPermissionService sysPermissionService;

    @Override
    public IPage<SysRole> selectList(Integer pageNum, Integer pageSize, SysRole sysRole) {
        Page<SysRole> page = new Page<>(pageNum, pageSize);
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(sysRole.getName())) {
            queryWrapper.like("name", sysRole.getName());
        }
        queryWrapper.eq("deleted",0);
        return sysRoleMapper.selectPage(page, queryWrapper);
    }

    @Override
    public List<SysRole> selectByUserId(Long userId) {
        return sysRoleMapper.selectByUserId(userId);
    }

    @Override
    public SysRole selectByName(String name, Long id) {
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        if (id != null) {
            queryWrapper.ne("id", id);
        }
        return sysRoleMapper.selectOne(queryWrapper);
    }

    @Override
    public Integer addOrUpdate(SysRole sysUser) {
        if (sysUser.getId() == null) {
            sysRoleMapper.insert(sysUser);
        } else {
            sysRoleMapper.updateById(sysUser);
            QueryWrapper<SysPermission> query = new QueryWrapper<SysPermission>().eq("role_id", sysUser.getId());
            sysPermissionService.remove(query);
        }

        //保存权限关系
        List<SysMenu> list = sysUser.getMenuList();
        List<SysPermission> sysPermissionList = new ArrayList<>();
        List<SysPermission> buttonPermissionList = new ArrayList<>();
        List<SysPermission> menuChildPermissionList = new ArrayList<>();
        for (SysMenu parent : list) {
            //判断孩子有没有保存过
            boolean isSave = false;
            for (SysMenu child : parent.getChildren()) {
                //已配置权限
                if (child.getActions() != null && child.getActions().size() > 0) {
                    isSave = true;
                    List<SysButton> buttonlist = child.getButtonList();
                    for (String action : child.getActions()) {
                        //保存按钮级权限
                        SysPermission button = new SysPermission();
                        button.setRoleId(sysUser.getId());//角色id
                        button.setAccessType(2);//按钮
                        button.setAccessSign(action);//按钮权限值
                        button.setAccessId(ApplicationUtils.getButtonId(buttonlist, action));
                        buttonPermissionList.add(button);
                    }
                    //保存2级菜单
                    SysPermission menuChild = new SysPermission();
                    menuChild.setRoleId(sysUser.getId());//角色id
                    menuChild.setAccessType(1);//菜单
                    menuChild.setAccessId(child.getId());//菜单id
                    menuChild.setAccessSign(child.getKeySign());//菜单权限值
                    menuChildPermissionList.add(menuChild);
                }
            }
            //保存一级菜单
            if (isSave) {
                SysPermission menuParent = new SysPermission();
                menuParent.setRoleId(sysUser.getId());//角色id
                menuParent.setAccessType(1);//菜单
                menuParent.setAccessId(parent.getId());//菜单id
                menuParent.setAccessSign(parent.getKeySign());//菜单权限值
                sysPermissionList.add(menuParent);
            }
        }

        if (buttonPermissionList.size() > 0) {
            sysPermissionService.saveBatch(buttonPermissionList);
        }
        if (menuChildPermissionList.size() > 0) {
            sysPermissionService.saveBatch(menuChildPermissionList);
        }
        if (sysPermissionList.size() > 0) {
            sysPermissionService.saveBatch(sysPermissionList);
        }
        return 1;
    }
}
